package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.FeeBillBiz;
import com.egao.orderB.entity.FeeBill;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-08-17 10:08:53
 * @version ：1.0
 */
@Repository
public class FeeBillBizImpl extends BaseDao implements FeeBillBiz {

	@Override
	public JsonResult list(PageResult2<T> page, FeeBill feeBill, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<FeeBill> rm = new BeanPropertyRowMapper<FeeBill>(FeeBill.class);
		StringBuffer sql = new StringBuffer("select f.id,f.organization_id,f.billIds");
		sql.append(",f.customerId,f.customerName,f.charge_time");
		sql.append(",f.bankReceiptPic,f.stateS,f.stateF,f.remark,f.create_time,f.create_user");
		sql.append(",f.update_time,f.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_fee_bill f ");
        sql.append(" inner join sys_user suser on suser.user_id=f.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=f.organization_id ");
        sql.append(" where f.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and f.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(feeBill)){
			if(Utils.isNotEmpty(feeBill.getCustomerName())){
				sql.append(" and instr(f.customerName,:customerName)>0 ");
				map.put("customerName", feeBill.getCustomerName());
			}
			if(Utils.isNotEmpty(feeBill.getCustomerId())){
				sql.append(" and instr(f.customerId,:customerId)>0 ");
				map.put("customerId", feeBill.getCustomerId());
			}
			if(Utils.isNotEmpty(feeBill.getDate1())){
				sql.append(" and f.charge_time>= :date1 ");
				map.put("date1", feeBill.getDate1());
			}
			if(Utils.isNotEmpty(feeBill.getDate2())){
				sql.append(" and f.charge_time<=:date2 ");
				map.put("date2", feeBill.getDate2());
			}
			if(Utils.isNotEmpty(feeBill.getStateS())){
				sql.append(" and instr(f.stateS,:stateS)>0 ");
				map.put("stateS", feeBill.getStateS());
			}
			if(Utils.isNotEmpty(feeBill.getStateF())){
				sql.append(" and instr(f.stateF,:stateF)>0 ");
				map.put("stateF", feeBill.getStateF());
			}
			if(Utils.isNotEmpty(feeBill.getSort())&&Utils.isNotEmpty(feeBill.getOrder())){
                sql.append(" order by f."+feeBill.getSort()+" "+feeBill.getOrder());
            } else {
                sql.append(" order by f.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<FeeBill> list(FeeBill feeBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<FeeBill> rm = new BeanPropertyRowMapper<FeeBill>(FeeBill.class);
        StringBuffer sql = new StringBuffer("select f.id,f.organization_id,f.billIds");
		sql.append(",f.customerId,f.customerName,f.charge_time");
        sql.append(",f.bankReceiptPic,f.stateS,f.stateF,f.remark,f.create_time,f.create_user");
		sql.append(",f.update_time,f.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_fee_bill f ");
        sql.append(" inner join sys_user suser on suser.user_id=f.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=f.organization_id ");
        sql.append(" where f.deleted=0 and sogzn.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and f.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(feeBill)){
			if(Utils.isNotEmpty(feeBill.getCustomerName())){
				sql.append(" and instr(f.customerName,:customerName)>0 ");
				map.put("customerName", feeBill.getCustomerName());
			}
			if(Utils.isNotEmpty(feeBill.getStateS())){
				sql.append(" and instr(f.stateS,:stateS)>0 ");
				map.put("stateS", feeBill.getStateS());
			}
			if(Utils.isNotEmpty(feeBill.getStateF())){
				sql.append(" and instr(f.stateF,:stateF)>0 ");
				map.put("stateF", feeBill.getStateF());
			}
            if(Utils.isNotEmpty(feeBill.getSort())&&Utils.isNotEmpty(feeBill.getOrder())){
				sql.append(" order by f."+feeBill.getSort()+" "+feeBill.getOrder());
			} else {
				sql.append(" order by f.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public FeeBill getById(FeeBill feeBill) {
		RowMapper<FeeBill> rm = new BeanPropertyRowMapper<FeeBill>(FeeBill.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select f.id,f.organization_id,f.billIds");
		sql.append(",f.customerId,f.customerName,f.charge_time");
        sql.append(",f.bankReceiptPic,f.stateS,f.stateF,f.remark,f.create_time,f.create_user");
		sql.append(",f.update_time,f.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_fee_bill f ");
        sql.append(" inner join sys_user suser on suser.user_id=f.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=f.organization_id ");
        sql.append(" where f.deleted=0 and sogzn.deleted=0 and f.id=:id ");
		map.put("id", feeBill.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(FeeBill feeBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_fee_bill (");
        sql.append("organization_id,billIds,bankReceiptPic,stateS,stateF,remark");
		sql.append(",customerId,customerName,charge_time");
		sql.append(",create_user,create_time) values ( ");
        sql.append(":organization_id,:billIds,:bankReceiptPic,:stateS,:stateF,:remark");
		sql.append(",:customerId,:customerName,:charge_time");
		sql.append(",:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("billIds", feeBill.getBillIds());
        map.put("customerId", feeBill.getCustomerId());
        map.put("customerName", feeBill.getCustomerName());
        map.put("charge_time", feeBill.getChargeTime());
        map.put("bankReceiptPic", feeBill.getBankReceiptPic());
        map.put("stateS", feeBill.getStateS());
        map.put("stateF", feeBill.getStateF());
        map.put("remark", feeBill.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(FeeBill feeBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_fee_bill set ");
        sql.append("billIds=:billIds,bankReceiptPic=:bankReceiptPic,charge_time=:charge_time ");
        sql.append(",stateS=:stateS,stateF=:stateF,remark=:remark ");
//        sql.append(",customerId=:customerId,customerName=:customerName ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
		map.put("billIds", feeBill.getBillIds());
		map.put("bankReceiptPic", feeBill.getBankReceiptPic());
		map.put("charge_time", feeBill.getChargeTime());
        map.put("stateS", feeBill.getStateS());
        map.put("stateF", feeBill.getStateF());
        map.put("remark", feeBill.getRemark());
//		map.put("customerId", feeBill.getCustomerId());
//		map.put("customerName", feeBill.getCustomerName());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", feeBill.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateStateS(FeeBill feeBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_fee_bill set ");
        sql.append("stateS=:stateS ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("stateS", feeBill.getStateS());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", feeBill.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateStateF(FeeBill feeBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_fee_bill set ");
        sql.append("stateF=:stateF ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("stateF", feeBill.getStateF());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", feeBill.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(FeeBill feeBill, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(feeBill.getId().length()>0){
			for(String ID : feeBill.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_fee_bill set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
